<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Airmail
 *
 * @author ketu
 */
class Zhishidi_Airmail_Model_Carrier_Airmail extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface {

    protected $_code = 'zhishidi_airmail';

    public function __construct() {
        parent::__construct();
    }

    public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $package_weight = $request->getPackageWeight() * $this->getConfigData('package_weight');

        if ($package_weight > $this->getConfigData('max_package_weight')) {
            return false;
        }

        $freeBoxes = 0;
        if ($request->getAllItems()) {
            foreach ($request->getAllItems() as $item) {

                if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                    continue;
                }

                if ($item->getHasChildren() && $item->isShipSeparately()) {
                    foreach ($item->getChildren() as $child) {
                        if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                            $freeBoxes += $item->getQty() * $child->getQty();
                        }
                    }
                } elseif ($item->getFreeShipping()) {
                    $freeBoxes += $item->getQty();
                }
            }
        }
        $this->setFreeBoxes($freeBoxes);


        $shippingPrice = ($package_weight / 1000) * $this->getConfigData('shipping_cost_unit');

        $result = Mage::getModel('shipping/rate_result');

        $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
        if ($shippingPrice !== false) {
            $method = Mage::getModel('shipping/rate_result_method');

            $method->setCarrier($this->_code);
            $method->setCarrierTitle($this->getConfigData('title'));

            $method->setMethod($this->_code);
            $method->setMethodTitle($this->getConfigData('name'));

            if ($request->getFreeShipping() === true || $this->getConfigData('allow_free_shipping_promotions') || $request->getPackageQty() == $this->getFreeBoxes() || ($this->getConfigData('enable_free_shipping_threshold') && $this->getConfigData('free_shipping_threshold') >0 && $request->getPackageValue()>$this->getConfigData('free_shipping_threshold'))) {
                $shippingPrice = '0.00';
                $method->setMethodTitle($this->getConfigData('free_method_text'));
            }

            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);

            $result->append($method);
        }
        return $result;
    }

    public function getAllowedMethods() {
        return array($this->_code => $this->getConfigData('name'));
    }

}